package cn.smbms.controller;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.smbms.bean.model.Staff;
import cn.smbms.biz.IStaffBiz;

@Controller
@RequestMapping("/global")
public class GlobalController {
	@Resource(name = "staffBiz")
	private IStaffBiz biz;
	
	//获取登录Ip方法
	public static String getIpAddr(HttpServletRequest request) {
          String ip = request.getHeader("x-forwarded-for");
          if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getHeader("Proxy-Client-IP");
         }
         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getHeader("WL-Proxy-Client-IP");
         }
         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             ip = request.getRemoteAddr();
         }
         if (ip.equals("0:0:0:0:0:0:0:1")) {
             ip = "本地";
        }
      return ip;
     }
	
	//登录验证
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpSession session,Model model) throws UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		Staff staff = new Staff();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		staff.setStaffNum(username);
		staff.setPwd(password);
		if(biz.checkLogin(staff) != null){
			staff = biz.findStaffByStaffNum(username);
			staff.setLoginIP(getIpAddr(request));
			staff.setLoginStatus(2);
			biz.update(staff);
			session.setAttribute("staff", staff);
			return "redirect:/global/index";
		}else if(username != null && password != null){
			session.setAttribute("staff", staff);
			model.addAttribute("msg", "用户名或密码错误！");
			return "/login";
		}else{
			return "/login";
		}
	}
	
	//退出
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpSession session) throws UnsupportedEncodingException{
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null){
			staff.setLoginStatus(1);
			biz.update(staff);
		}
		session.removeAttribute("staff");
		request.setAttribute("msg","您已退出系统！");
		return "/login";
	}
	
	//进入首页
	@RequestMapping("/index")
	public String index(){
		return "/index";
	}
	
	//根据请求进入相应的页面
	@RequestMapping("/page")
	public String goPage(HttpServletRequest request,Model model){
		String flag = request.getParameter("flag");
		String src = "";
		if("functionAuthority".equals(flag)){
			src = "functionAuthority/index";
		}else if("sysRole".equals(flag)){
			src = "sysRole/index";
		}else if("roleAuthority".equals(flag)){
			src = "roleAuthority/index";
		}else if("userRole".equals(flag)){
			src = "userRole/index";
		}else{
			src = "staff/index";
		}
		return src;
	}
}